// Geometric Tools, Inc.
// http://www.geometrictools.com
// Copyright (c) 1998-2006.  All Rights Reserved
//
// The Wild Magic Library (WM3) source code is supplied under the terms of
// the license agreement
//     http://www.geometrictools.com/License/WildMagic3License.pdf
// and may not be copied or disclosed except in accordance with the terms
// of that agreement.

#include "Wm3FoundationPCH.h"
#include "Wm3CollapseRecordArray.h"
using namespace Wm3;

WM3_IMPLEMENT_RTTI(Wm3,CollapseRecordArray,Object);
WM3_IMPLEMENT_STREAM(CollapseRecordArray);
WM3_IMPLEMENT_DEFAULT_NAME_ID(CollapseRecordArray,Object);

//----------------------------------------------------------------------------
CollapseRecordArray::CollapseRecordArray (int iQuantity,
    CollapseRecord* akArray)
{
    m_iQuantity = iQuantity;
    m_akArray = akArray;
}
//----------------------------------------------------------------------------
CollapseRecordArray::CollapseRecordArray (const CollapseRecordArray& rkShared)
{
    m_akArray = 0;
    *this = rkShared;
}
//----------------------------------------------------------------------------
CollapseRecordArray::~CollapseRecordArray ()
{
    WM3_DELETE[] m_akArray;
}
//----------------------------------------------------------------------------
CollapseRecordArray& CollapseRecordArray::operator= (
    const CollapseRecordArray& rkShared)
{
    WM3_DELETE[] m_akArray;
    m_iQuantity = rkShared.m_iQuantity;
    if (rkShared.m_akArray)
    {
        m_akArray = WM3_NEW CollapseRecord[m_iQuantity];
        for (int i = 0; i < m_iQuantity; i++)
        {
            m_akArray[i] = rkShared.m_akArray[i];
        }
    }
    else
    {
        m_akArray = 0;
    }
    return *this;
}
//----------------------------------------------------------------------------

//----------------------------------------------------------------------------
// streaming
//----------------------------------------------------------------------------
void CollapseRecordArray::Load (Stream& rkStream, Stream::Link* pkLink)
{
    WM3_BEGIN_DEBUG_STREAM_LOAD;

    Object::Load(rkStream,pkLink);
    rkStream.Read(m_iQuantity);
    m_akArray = WM3_NEW CollapseRecord[m_iQuantity];

    for (int i = 0; i < m_iQuantity; i++)
    {
        rkStream.Read(m_akArray[i].VKeep);
        rkStream.Read(m_akArray[i].VThrow);
        rkStream.Read(m_akArray[i].VQuantity);
        rkStream.Read(m_akArray[i].TQuantity);
        rkStream.Read(m_akArray[i].IQuantity);
        if (m_akArray[i].IQuantity > 0)
        {
            m_akArray[i].Indices = WM3_NEW int[m_akArray[i].IQuantity];
            rkStream.Read(m_akArray[i].IQuantity,m_akArray[i].Indices);
        }
        else
        {
            m_akArray[i].Indices = 0;
        }
    }

    WM3_END_DEBUG_STREAM_LOAD(CollapseRecordArray);
}
//----------------------------------------------------------------------------
void CollapseRecordArray::Link (Stream& rkStream, Stream::Link* pkLink)
{
    Object::Link(rkStream,pkLink);
}
//----------------------------------------------------------------------------
bool CollapseRecordArray::Register (Stream& rkStream) const
{
    return Object::Register(rkStream);
}
//----------------------------------------------------------------------------
void CollapseRecordArray::Save (Stream& rkStream) const
{
    WM3_BEGIN_DEBUG_STREAM_SAVE;

    Object::Save(rkStream);
    rkStream.Write(m_iQuantity);

    for (int i = 0; i < m_iQuantity; i++)
    {
        rkStream.Write(m_akArray[i].VKeep);
        rkStream.Write(m_akArray[i].VThrow);
        rkStream.Write(m_akArray[i].VQuantity);
        rkStream.Write(m_akArray[i].TQuantity);
        rkStream.Write(m_akArray[i].IQuantity);
        if (m_akArray[i].IQuantity > 0)
        {
            rkStream.Write(m_akArray[i].IQuantity,m_akArray[i].Indices);
        }
    }

    WM3_END_DEBUG_STREAM_SAVE(CollapseRecordArray);
}
//----------------------------------------------------------------------------
int CollapseRecordArray::GetMemoryUsed () const
{
    int iSize = sizeof(CollapseRecordArray) - sizeof(Object) +
        Object::GetMemoryUsed();

    for (int i = 0; i < m_iQuantity; i++)
    {
        iSize += m_akArray[i].IQuantity*sizeof(int);
    }
    return iSize;
}
//----------------------------------------------------------------------------
int CollapseRecordArray::GetDiskUsed (const StreamVersion& rkVersion) const
{
    int iSize = Object::GetDiskUsed(rkVersion) +
        sizeof(m_iQuantity);

    for (int i = 0; i < m_iQuantity; i++)
    {
        iSize += sizeof(m_akArray[i].VKeep);
        iSize += sizeof(m_akArray[i].VThrow);
        iSize += sizeof(m_akArray[i].VQuantity);
        iSize += sizeof(m_akArray[i].TQuantity);
        iSize += sizeof(m_akArray[i].IQuantity);
        iSize += m_akArray[i].IQuantity*sizeof(m_akArray[i].Indices[0]);
    }

    return iSize;
}
//----------------------------------------------------------------------------
StringTree* CollapseRecordArray::SaveStrings (const char*)
{
    StringTree* pkTree = WM3_NEW StringTree(1,2);

    // strings
    pkTree->SetString(0,Format(&TYPE,GetName()));

    // children
    pkTree->SetChild(0,Object::SaveStrings());

    StringTree* pkChild = WM3_NEW StringTree(m_iQuantity+2,0);
    pkChild->SetString(0,Format("records"));
    pkChild->SetString(1,Format("quantity =",m_iQuantity));
    for (int i = 0; i < m_iQuantity; i++)
    {
        const CollapseRecord& rkRecord = m_akArray[i];
        const size_t uiSize = 128;
        char* acString = WM3_NEW char[uiSize];
        System::Sprintf(acString,uiSize,
            "%d: keep = %d, throw = %d, vq = %d, tq = %d, iq = %d",i,
            rkRecord.VKeep,rkRecord.VThrow,rkRecord.VQuantity,
            rkRecord.TQuantity,rkRecord.IQuantity);

        pkChild->SetString(i+2,acString);
    }
    pkTree->SetChild(1,pkChild);

    return pkTree;
}
//----------------------------------------------------------------------------
